package com.jic.veteran.portal.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.jic.veteran.mapper.ProjectBasicMapper;
import com.jic.veteran.mapper.ProjectMessageMapper;
import com.jic.veteran.model.ProjectBasic;
import com.jic.veteran.model.ProjectBasicExample;
import com.jic.veteran.model.ProjectMessage;
import com.jic.veteran.model.ProjectMessageExample;
import com.jic.veteran.portal.component.VeteranSecurityContext;
import com.jic.veteran.portal.dao.ProjectMessageDao;
import com.jic.veteran.portal.domain.ProjectMessageListDto;
import com.jic.veteran.portal.service.ProjectMessageService;
import com.jic.veteran.portal.service.UmsMemberCacheService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProjectMessageServiceImpl implements ProjectMessageService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectMessageServiceImpl.class);
    @Autowired
    private ProjectMessageMapper projectMessageMapper;
    @Autowired
    private ProjectBasicMapper projectBasicMapper;
    @Autowired
    private UmsMemberCacheService umsMemberCacheService;
    @Autowired private ProjectMessageDao projectMessageDao;

    @Override
    public List<ProjectMessageListDto> list(String projectId, Integer pageSize, Integer pageNum) {
      PageHelper.startPage(pageNum,pageSize);
      return projectMessageDao.selectByProjectId(projectId, true);
    }

    @Override
    public List<ProjectMessage> myList(String openid, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        ProjectMessageExample example = new ProjectMessageExample();
        if(StrUtil.isNotEmpty(openid)) {
            example.createCriteria().andUserIdEqualTo(openid );
        }
        return projectMessageMapper.selectByExample(example);
    }

    @Override
    public int create(String projectId,ProjectMessage projectMessage) {
        String dto = umsMemberCacheService.getProjectByProjectId(projectId);
        if(null != dto){
            JSONObject jsonObject = JSONUtil.parseObj(dto);
            if(null != jsonObject &&jsonObject.containsKey("projectBasic")) {
                JSONObject projectBasic = jsonObject.getJSONObject("projectBasic");
                if(null != projectBasic &&projectBasic.containsKey("projectName")) {
                    projectMessage.setProjectName(projectBasic.getStr("projectName"));
                }
                projectMessage.setProjectId(projectId);
            }
            projectMessage.setCreateBy(projectMessage.getUserId());
            projectMessage.setCreateTime(DateUtil.date());
            projectMessage.setModifyBy(projectMessage.getUserId());
            projectMessage.setModifyTime(DateUtil.date());
            return projectMessageMapper.insert(projectMessage);
        } else {
            ProjectBasicExample projectBasicExample = new ProjectBasicExample();
            projectBasicExample.createCriteria().andProjectIdEqualTo(projectId);
            List<ProjectBasic> projectBasics = projectBasicMapper.selectByExample(projectBasicExample);
            if(!projectBasics.isEmpty()) {
                projectMessage.setProjectName(projectBasics.get(0).getProjectName());
                projectMessage.setProjectId(projectBasics.get(0).getProjectId());
                projectMessage.setCreateBy(projectMessage.getUserId());
                projectMessage.setCreateTime(DateUtil.date());
                projectMessage.setModifyBy(projectMessage.getUserId());
                projectMessage.setModifyTime(DateUtil.date());
                return projectMessageMapper.insert(projectMessage);
            }
        }
        return 0;
    }

    @Override
    public int update(Long id,ProjectMessage projectMessage) {
        projectMessage.setId(id);
        projectMessage.setModifyBy(VeteranSecurityContext.getUserName());
        projectMessage.setModifyTime(DateUtil.date());
        return projectMessageMapper.updateByPrimaryKeySelective(projectMessage);
    }

    @Override
    public ProjectMessage getItem(Long id) {
        return projectMessageMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(Long id) {
        return projectMessageMapper.deleteByPrimaryKey(id);
    }
}
